package com.sunda.spmsweb.ordercontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.dto.CategoryCreateDTO;
import com.sunda.spmsorder.dto.CategoryUpdateDTO;
import com.sunda.spmsorder.service.IMaterialCategoryService;
import com.sunda.spmsweb.aspect.Operation;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 物料分类表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2020-11-13
 */
@RestController
@Api(tags = "备件目录数据接口", description = "备件目录数据接口")
@RequestMapping("/materialCategory")
public class MaterialCategoryController {

    @Autowired
    IMaterialCategoryService iMaterialCategoryService;

    @Operation(logContent = "查询备件目录", operateType = "查询")
    @RequestMapping("/getCategoryList")
    @ApiOperation(value = "获取目录简要数据",notes = "获取目录简要数据，用于目录呈现", httpMethod = "GET")
    public ResponseResult getCategoryList() {
        try {
            return ResponseResult.success().add("msg", iMaterialCategoryService.getMaterialCategoryList());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求目录数据失败").add("msg", e);
        }
    }

    @RequestMapping("/getCategoryDtlList")
    @ApiOperation(value = "获取请购商城目录详情数据",notes = "获取请购商城目录详情数据，用于目录维护", httpMethod = "GET")
    public ResponseResult getCategoryDtlList() {
        try {
            return ResponseResult.success().add("msg", iMaterialCategoryService.getMaterialCategoryDtlList());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求目录数据失败").add("msg", e);
        }
    }

    @RequestMapping("/getCategoryMaterial")
    @ApiOperation(value = "根据目录编号查询物料信息", notes = "根据目录编号查询物料信息\n示例参数：categoryId = 100101", httpMethod = "POST")
    public ResponseResult getCategoryMaterial(String categoryId,
                                              @RequestParam(defaultValue = "1") int pageNo,
                                              @RequestParam(defaultValue = "20") int pageSize){
        try {
            return ResponseResult.success().add("materialList", iMaterialCategoryService.getCategoryMaterial(categoryId, pageNo, pageSize));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("执行异常");
        }
    }


    @RequestMapping("/createCategoryMaterial")
    @ApiOperation(value = "新增/更新物料目录信息", notes = "传入目录 ID，物料编号关联关系存储，两个值都必输；\n" +
            "{\n" +
            "\t\"materialSpmsList\": [{\n" +
            "\t\t\"categoryId\": \"100608\",\n" +
            "\t\t\"materialNo\": \"170001240\"\n" +
            "\t}, {\n" +
            "\t\t\"categoryId\": \"100608\",\n" +
            "\t\t\"materialNo\": \"170001241\"\n" +
            "\t}]\n" +
            "}\n", httpMethod = "POST")
    @ApiImplicitParam(name = "jsonObject", value = "JSONObject 示例字符串", dataTypeClass = String.class, required = true, paramType = "body", dataType = "string")
    public ResponseResult createCategoryMaterial(@RequestBody JSONObject jsonObject){
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iMaterialCategoryService.createCategoryMaterial(jsonObject, userId);
    }

    @RequestMapping("/deleteCategoryMaterial")
    @ApiOperation(value = "删除目录-物料关系数据", notes = "根据 uuid list 删除目录-物料关系数据，真删除；\n" +
            "[\"e2eeae29cee14459a03ad4e8d69cf52a\", \"407e325fa473460fa7e321ba1985607c\"]", httpMethod = "POST")
    public ResponseResult deleteCategoryMaterial(@RequestBody String [] doc){
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iMaterialCategoryService.deleteCategoryMaterial(doc, userId);
    }



    @RequestMapping("createCategory")
    @ApiOperation(value = "新增目录信息", notes = "新增目录信息\n" +
            "parentId 父分类ID(一级分类的父分类ID为 0 )必输；categoryRank为0~1000的数字，越大越靠前；isDeleted 默认0有效，1无效，默认0；中英文名称都必输。\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"categoryENName\": \"categoryENName\",\n" +
            "\t\"categoryLevel\": 2,\n" +
            "\t\"categoryName\": \"物料二级分类\",\n" +
            "\t\"categoryRank\": 0,\n" +
            "\t\"parentId\": 10\n" +
            "}", httpMethod = "POST")
    public ResponseResult createCategory(@RequestBody CategoryCreateDTO categoryCreateDTO){
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iMaterialCategoryService.createCategory(categoryCreateDTO, userId);
    }

    @RequestMapping("updateCategory")
    @ApiOperation(value = "更新目录信息", notes = "更新目录信息\n" +
            "仅能更新中英文描述及排序；\n" +
            "示例参数：\n" +
            "[\n" +
            "\t{\n" +
            "\t\t\"categoryENName\": \"categoryENName\",\n" +
            "\t\t\"categoryId\": 1010,\n" +
            "\t\t\"categoryName\": \"物料二级分类\",\n" +
            "\t\t\"categoryRank\": 1\n" +
            "\t}\n" +
            "]", httpMethod = "POST")
    public ResponseResult updateCategory(@RequestBody List<CategoryUpdateDTO> categoryUpdateDTOList){
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iMaterialCategoryService.updateCategory(categoryUpdateDTOList, userId);
    }

    @RequestMapping("deleteCategory")
    @ApiOperation(value = "停用或启用目录信息", notes = "停用或启用目录信息\n" +
            "停用或启用 请购商城目录分类数据，isDeleted = 1 删除，isDeleted = 0 启用；\n" +
            "示例参数：\n" +
            "categoryId = 1010, isDeleted = 0", httpMethod = "POST")
    public ResponseResult deleteCategory(@RequestParam Integer categoryId, @RequestParam String isDeleted){
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iMaterialCategoryService.deleteCategory(categoryId, isDeleted, userId);
    }

    @RequestMapping("getDeletedCategory")
    @ApiOperation(value = "获取已停用目录信息", notes = "获取已停用目录信息\n" +
            "", httpMethod = "GET")
    public ResponseResult getDeletedCategory(){
        return iMaterialCategoryService.getDeletedCategory();
    }

}
